package com.sxpi.zixun.controller.state;

import com.alibaba.fastjson.JSONObject;
import com.sxpi.zixun.controller.BaseController;
import com.sxpi.zixun.service.state.IStateService;
import lombok.RequiredArgsConstructor;
import org.omg.PortableInterceptor.INACTIVE;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.Map;
import java.util.concurrent.*;

/**
 * @Author: 董伟豪
 * @Date: 2022/9/18 12:45
 */
@RestController
@RequiredArgsConstructor
public class StateController extends BaseController {

    private final IStateService iStateService;

    /**
     * 统计订单和用户相关
     * @return
     */
    @PostMapping("/state/orderuser")
    public Map<String,Object> countIndexData(){
        return iStateService.countIndexData();
    }

    ExecutorService executorService = Executors.newCachedThreadPool();

    /**
     * 统计首页相关的所有信息
     * 但是还是会存在一个主线程
     * @return
     */
    @PostMapping("/state/indexdatadeprecated")
    @Deprecated
    public JSONObject stateIndexDataDeprecated(){
        // 闭锁（一堵墙壁）
        CountDownLatch countDownLatch = new CountDownLatch(3);
        // 返回的数据
        JSONObject jsonObject = new JSONObject();
        try {
            // 线程一
            executorService.submit(()->{
                // 统计操作系统
                List<Map<String,Object>> osList = iStateService.stateOS(null);
                jsonObject.put("osList",osList);
                countDownLatch.countDown();
            });
            // 线程二
            executorService.submit(()->{
                // 统计浏览器
                List<Map<String,Object>> browerList = iStateService.stateRequestBrower(null);
                jsonObject.put("browerList",browerList);
                countDownLatch.countDown();
            });
            // 线程三
            executorService.submit(()->{
                // 统计性别
                List<Map<String,Object>> userSexList = iStateService.stateUserSex(null);
                jsonObject.put("userSexList",userSexList);
                countDownLatch.countDown();
            });
            countDownLatch.await();
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        // 但是返回的结果会有可能是空的
        return jsonObject;
    }

    @PostMapping("/state/indexdata")
    public JSONObject stateIndexData() throws ExecutionException, InterruptedException, TimeoutException {

        // 线程一
        Future<List<Map<String, Object>>> future1 = executorService.submit(()->{
            // 统计操作系统
            List<Map<String,Object>> osList = iStateService.stateOS(null);
            return osList;
        });
        // 线程二
        Future<List<Map<String, Object>>> future2 = executorService.submit(()->{
            // 统计浏览器
            List<Map<String,Object>> browerList = iStateService.stateRequestBrower(null);
            return browerList;
        });

        // 线程三
        Future<List<Map<String, Object>>> future3 = executorService.submit(() -> {
            // 统计性别
            List<Map<String, Object>> userSexList = iStateService.stateUserSex(null);
            return userSexList;
        });

        // 线程四
        Future<List<Map<String, Object>>> future4 = executorService.submit(() -> {
            // 统计性别
            List<Map<String, Object>> userSexList = iStateService.stateOrderRealtion(null);
            return userSexList;
        });
        // Future.get()里面也可以添加超时时间
        List<Map<String, Object>> map1 = future1.get(10L,TimeUnit.SECONDS);
        List<Map<String, Object>> map2 = future2.get(10L,TimeUnit.SECONDS);
        List<Map<String, Object>> map3 = future3.get(10L,TimeUnit.SECONDS);
        List<Map<String, Object>> map4 = future4.get(10L,TimeUnit.SECONDS);

        // 返回的数据
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("osLists",map1);
        jsonObject.put("broswerLists",map2);
        jsonObject.put("userSexLists",map3);
        jsonObject.put("orderLists",map4);

        // 但是返回的结果会有可能是空的
        return jsonObject;
    }

    /**
     * 根据条件查询订单统计信息
     * 0: 全部
     * 1: 今天
     * 2: 昨天
     * 3：近三天
     * 4：近一周
     * 5：上个月
     * 6：本月
     * @param type
     * @return
     */
    @PostMapping("/state/order/{type}/{ctype}")
    public List<Map<String,Object>> stateOrderByTime(@PathVariable("type")Integer type,
                                                     @PathVariable("ctype")Integer ctype){
        String condition = null;
        if(type == 1){
            // 今天
            condition = "TO_DAYS(create_time) = TO_DAYS(NOW())";
        }else if(type == 2){
            // 昨天
            condition = "TO_DAYS(NOW()) - TO_DAYS(create_time) <= 1";
        }else if(type == 3){
            // 近三天
            condition = "DATE_SUB(NOW(),INTERVAL 3 DAY) <= date(create_time)";
        }
        else if(type == 4){
            // 近一周
            condition = "DATE_SUB(NOW(),INTERVAL 7 DAY) <= date(create_time)";
        }
        else if(type == 5){
            // 上一个月
            condition = "PERIOD_DIFF(DATE_FORMAT(now(),'%Y%M'),DATE_FORMAT(create_time,'%Y%M')) = 1";
        }else if(type == 6){
            // 本月
            condition = "DATE_FORMAT(create_time,'%Y%M') = DATE_FORMAT(NOW(),'%Y%M')";
        }
        List<Map<String,Object>> mapList = null;
        if(ctype.equals(1)){
            // 根据时间统计订单
            mapList = iStateService.stateOrderRealtion(condition);
        }else if(ctype.equals(2)){
            // 根据时间统计操作系统
            mapList = iStateService.stateOS(condition);
        }else if(ctype.equals(3)){
            // 根据时间统计浏览器
            mapList = iStateService.stateRequestBrower(condition);
        } else if(ctype.equals(4)){
            // 根据时间统计性别
            mapList = iStateService.stateUserSex(condition);
        }

        return mapList;
    }


}
